<?php

class Modules_Gallery extends Module {

	public $title = "Галереи";
	public $name = "gallery";

	protected function _actionproperties(Blocks $block) {
		
	}

	public function actions() {
		
	}

	public function mainActions() {
		
	}

	public static function subMenu() {
		return array(
			array(
				'url' => '/admin.php?module=gallery&action=gallery',
				'title' => 'Галереи',
				'icon' => '/tpl/admin/images/inbox-image.png',
			),
		);
	}

	public function adminActionIndex() {
		return $this->adminActionGallery();
	}

	public function adminActionGallery() {
		$galleries = Doctrine_Query::create()
				->from('Gallery')
				->execute();
		$users = Doctrine_Query::create()
				->from('Users')
				->execute();
		$_gallery = new Gallery();

		return $this->renderTpl('gallery_list.html', array(
			'_gallery' => $_gallery,
			'galleries' => $galleries,
			'users' => $users,
		));
	}

	public function adminActionGallery_save() {
		$data = isset($_REQUEST['gallery']) ? $_REQUEST['gallery'] : array();
		$data['id'] = (int) $data['id'];

		$gallery = $data['id'] ? Doctrine_Core::getTable('Gallery')->find($data['id']) : new Gallery();
		unset($data['id']);

		$gallery->fromArray($data);
		$gallery->save();

		Request::redirect(-1);
	}

	public function adminActionImages() {
		$gallery = (!empty($_REQUEST['gallery'])) ? $_REQUEST['gallery'] : 0;
		if (!$gallery) Request::redirect(404);

		$gallery = Doctrine_Core::getTable('Gallery')->find($gallery);
		if (!$gallery) Request::redirect(404);

		$_image = new GalleryImages();
		$_image->Gallery->id = $gallery->id;

		$users = Doctrine_Query::create()->from("Users")->execute();

		return $this->renderTpl('images.html', array(
			'gallery' => $gallery,
			'images' => $gallery->Images,
			'_image' => $_image,
			'users' => $users,
		));
	}

	public function adminActionImage_save() {
		$data = isset($_REQUEST['image']) ? $_REQUEST['image'] : array();
		$data['id'] = (int) $data['id'];
		$new = !$data['id'];

		$image = !$new ? Doctrine_Core::getTable("GalleryImages")->find($data['id']) : new GalleryImages();
		unset($data['id']);
		$image->fromArray($data);
		$image->save();

		if ($new && !empty($_FILES['file']['error'])) {
			throw new Exception("You must select image");
			return false;
		}

		if (empty($_FILES['file']['error'])) {
			$img = Image::create($_FILES['file']['tmp_name']);
			$ext = strtolower($img->getFormat());

			$image->img_ext = $ext;
			$ds = DIRECTORY_SEPARATOR;
			$this->chekImgDir(RP . "files{$ds}gallery{$ds}src");

			/* Оригинал */
			$src = RP . "files{$ds}gallery{$ds}src{$ds}{$image->id}.{$ext}";
			move_uploaded_file($_FILES['file']['tmp_name'], $src);

			/* Большое изображение */
			//$filename = RP . "files{$dr}{$this->name}{$dr}{$image->id}.$ext";
			//$img->resize(500, 1000);
			//$img->save($filename);

			/* Малое изображение */
			//$filename = RP . "files{$dr}{$this->name}{$dr}tumb_{$image->id}.$ext";
			//$img->resize(150, 150);
			//$img->save($filename);
		}
		$image->save();

		Request::redirect(-1);
	}

	public function adminActionImage_delete() {
		$id = isset($_REQUEST['id']) ? (int) $_REQUEST['id'] : 0;
		if (!$id) Request::redirect(404);

		$img = Doctrine_Core::getTable("GalleryImages")->find($id);
		if (!$img) Request::redirect(404);

		$ds = DIRECTORY_SEPARATOR;
		$src = RP . "files{$ds}gallery{$ds}src{$ds}{$img->id}.{$img->img_ext}";
		$files = glob(RP . "files{$ds}gallery{$ds}*x*{$ds}{$img->id}.{$img->img_ext}");
				
		foreach($files as $file) unlink ($file);
		unlink ($src);

		$row = $img->delete();
		return $row;
	}

	public function adminActionAva() {
		$id = isset($_REQUEST['id']) ? (int) $_REQUEST['id'] : 0;
		if (!$id) Request::redirect(404);

		$img = Doctrine_Core::getTable("GalleryImages")->find($id);
		if (!$img) Request::redirect(404);

		$img->ava = 1;
		$img->save();

		Request::redirect(-1);
	}

	public function adminActionUnava() {
		$id = isset($_REQUEST['id']) ? (int) $_REQUEST['id'] : 0;
		if (!$id) Request::redirect(404);

		$img = Doctrine_Core::getTable("GalleryImages")->find($id);
		if (!$img) Request::redirect(404);

		$img->ava = 0;
		$img->save();

		Request::redirect(-1);
	}
	
	public static function saveImage(){
		$file = RP . $_SERVER['REDIRECT_URL'];

		$ext = pathinfo($file, PATHINFO_EXTENSION);
		$id = (int) preg_replace("|tumb_|", "", pathinfo($file, PATHINFO_BASENAME));
		if (!$id || !$ext) {
			header("HTTP/1.1 404 Not Found");
			exit();
		}

		$ds = DIRECTORY_SEPARATOR;

		$src = RP . "files{$ds}gallery{$ds}src{$ds}{$id}.{$ext}";
		if (!file_exists($src)) {
			header("HTTP/1.1 404 Not Found");
			exit();
		}

		$dir = pathinfo($file, PATHINFO_DIRNAME);
		$size = preg_split("|x|", pathinfo($dir, PATHINFO_BASENAME));

		$w = (int) $size[0];
		$h = (int) $size[1];

		if (!$w || !$h) {
			header("HTTP/1.1 404 Not Found");
			exit();
		}
		
		$filename = RP . "files{$ds}gallery{$ds}{$w}x{$h}{$ds}";
		if (!file_exists($filename) || !is_dir($filename)) mkdir($filename, 0777, true);
		
		$Image = Image::create($src);
		$filename .= $id .".". $ext;
		$Image->resize($w, $h);
		$Image->save($filename);
		$Image->show();
	}
	
}